<?php
/**
 * Niushop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.niushop.com.cn
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 */

namespace app\model\web;


use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
use think\facade\Cache;

/**
 * PC端
 * @author Administrator
 *
 */
class Pc extends BaseModel
{

	private $link = [
		[
			'title' => '首页',
			'url' => '/index'
		],
		[
			'title' => '登录',
			'url' => '/login'
		],
		[
			'title' => '注册',
			'url' => '/register'
		],
		[
			'title' => '找回密码',
			'url' => '/find_pass'
		],
		[
			'title' => '公告列表',
			'url' => '/cms/notice'
		],
		[
			'title' => '帮助中心',
			'url' => '/cms/help'
		],
		[
			'title' => '购物车',
			'url' => '/cart'
		],
		[
			'title' => '品牌专区',
			'url' => '/brand'
		],
		[
			'title' => '领券中心',
			'url' => '/coupon'
		],
		[
			'title' => '商品分类',
			'url' => '/category'
		],
		[
			'title' => '团购专区',
			'url' => '/promotion/groupbuy'
		],
		[
			'title' => '秒杀专区',
			'url' => '/promotion/seckill'
		],
		[
			'title' => '会员中心',
			'url' => '/member/home/index'
		]

	];

	/**
	 * 获取PC端固定跳转链接
	 * @return array
	 */
	public function getLink()
	{
		foreach ($this->link as $k => $v) {
			if ($v[ 'url' ] == '/register' && addon_is_exit('memberregister') == 0) {
				unset($this->link[ $k ]);
			} elseif ($v[ 'url' ] == '/coupon' && addon_is_exit('coupon') == 0) {
				unset($this->link[ $k ]);
			} elseif ($v[ 'url' ] == '/promotion/seckill' && addon_is_exit('seckill') == 0) {
				unset($this->link[ $k ]);
			} elseif ($v[ 'url' ] == '/promotion/groupbuy' && addon_is_exit('groupbuy') == 0) {
				unset($this->link[ $k ]);
			}
		}
		$this->link = array_values($this->link);
		return $this->link;
	}

	/**
	 * 设置热门搜索关键词
	 * @param $data
	 * @return array
	 */
	public function setHotSearchWords($data)
	{
		$config = new ConfigModel();
		$res = $config->setConfig($data, 'PC端热门搜索关键词', 1, [ [ 'site_id', '=', 0 ], [ 'app_module', '=', 'admin' ], [ 'config_key', '=', 'PC_HOT_SEARCH_WORDS_CONFIG' ] ]);
		return $res;
	}

	/**
	 * 获取热门搜索关键词
	 * @param $data
	 * @return array
	 */
	public function getHotSearchWords()
	{
		$config = new ConfigModel();
		$res = $config->getConfig([ [ 'site_id', '=', 0 ], [ 'app_module', '=', 'admin' ], [ 'config_key', '=', 'PC_HOT_SEARCH_WORDS_CONFIG' ] ]);
		if (empty($res[ 'data' ][ 'value' ])) {
			$res[ 'data' ][ 'value' ] = [
				'words' => ''
			];
		}
		return $res;
	}

	/**
	 * 设置默认搜索关键词
	 * @param $data
	 * @return array
	 */
	public function setDefaultSearchWords($data)
	{
		$config = new ConfigModel();
		$res = $config->setConfig($data, 'PC端默认搜索关键词', 1, [ [ 'site_id', '=', 0 ], [ 'app_module', '=', 'admin' ], [ 'config_key', '=', 'PC_DEFAULT_SEARCH_WORDS_CONFIG' ] ]);
		return $res;
	}

	/**
	 * 获取默认搜索关键词
	 * @param $data
	 * @return array
	 */
	public function getDefaultSearchWords()
	{
		$config = new ConfigModel();
		$res = $config->getConfig([ [ 'site_id', '=', 0 ], [ 'app_module', '=', 'admin' ], [ 'config_key', '=', 'PC_DEFAULT_SEARCH_WORDS_CONFIG' ] ]);
		if (empty($res[ 'data' ][ 'value' ])) {
			$res[ 'data' ][ 'value' ] = [
				'words' => '搜索 商品/店铺'
			];
		}
		return $res;
	}

	/**
	 * 设置首页浮层
	 * @param $data
	 * @return array
	 */
	public function setFloatLayer($data)
	{
		$config = new ConfigModel();
		$res = $config->setConfig($data, 'PC端首页浮层', 1, [ [ 'site_id', '=', 0 ], [ 'app_module', '=', 'admin' ], [ 'config_key', '=', 'PC_INDEX_FLOAT_LAYER_CONFIG' ] ]);
		return $res;
	}

	/**
	 * 获取首页浮层
	 * @param $data
	 * @return array
	 */
	public function getFloatLayer()
	{
		$config = new ConfigModel();
		$res = $config->getConfig([ [ 'site_id', '=', 0 ], [ 'app_module', '=', 'admin' ], [ 'config_key', '=', 'PC_INDEX_FLOAT_LAYER_CONFIG' ] ]);
		if (empty($res[ 'data' ][ 'value' ])) {
			$res[ 'data' ][ 'value' ] = [
				'title' => '首页浮层',
				'url' => 'https://www.niushop.com.cn/',
				'is_show' => 1,
				'number' => '3',
				'img_url' => 'upload/default/pc/index_float_layer.png'
			];
		}
		return $res;
	}

	/****************************************************************导航******************************************/
	/**
	 * 添加导航
	 * @param array $data
	 */
	public function addNav($data)
	{
		$id = model('pc_nav')->add($data);
		Cache::tag("pc_nav")->clear();
		return $this->success($id);
	}

	/**
	 * 修改导航
	 * @param array $data
	 */
	public function editNav($data, $condition)
	{
		$res = model('pc_nav')->update($data, $condition);
		Cache::tag("pc_nav")->clear();
		return $this->success($res);
	}

	/**
	 * 删除导航
	 * @param unknown $coupon_type_id
	 */
	public function deleteNav($condition)
	{
		$res = model('pc_nav')->delete($condition);
		Cache::tag("pc_nav")->clear();
		return $this->success($res);
	}

	/**
	 * 获取导航详情
	 * @param int $id
	 * @return multitype:string mixed
	 */
	public function getNavInfo($id)
	{
		$cache = Cache::get("pc_nav_getNavInfo_" . $id);
		if (!empty($cache)) {
			return $this->success($cache);
		}
		$res = model('pc_nav')->getInfo([ [ 'id', '=', $id ] ], 'id, nav_title, nav_url, sort, is_blank, create_time, modify_time, nav_icon, is_show');
		Cache::tag("pc_nav")->set("pc_nav_getNavInfo_" . $id, $res);
		return $this->success($res);
	}

    /**
     * 获取导航详情
     * @param int $id
     * @return multitype:string mixed
     */
    public function getNavInfoByCondition($condition, $field = 'id, nav_title, nav_url, sort, is_blank, create_time, modify_time, nav_icon, is_show')
    {
        $res = model('pc_nav')->getInfo($condition, $field);
        return $this->success($res);
    }

	/**
	 * 获取导航分页列表
	 * @param array $condition
	 * @param number $page
	 * @param string $page_size
	 * @param string $order
	 * @param string $field
	 */
	public function getNavPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = 'id, nav_title, nav_url, sort, is_blank, create_time, modify_time, nav_icon, is_show')
	{
		$data = json_encode([ $condition, $field, $order, $page, $page_size ]);
		$cache = Cache::get("pc_nav_getNavPageList_" . $data);
		if (!empty($cache)) {
			return $this->success($cache);
		}
		$list = model('pc_nav')->pageList($condition, $field, $order, $page, $page_size);
		Cache::tag("pc_nav")->set("pc_nav_getNavList_" . $data, $list);
		return $this->success($list);
	}

	/**
	 * 修改排序
	 * @param int $sort
	 * @param int $id
	 */
	public function modifyNavSort($sort, $id)
	{
		$res = model('pc_nav')->update([ 'sort' => $sort ], [ [ 'id', '=', $id ] ]);
		Cache::tag('pc_nav')->clear();
		return $this->success($res);
	}

	/****************************************************************导航******************************************/


	/****************************************************************友情链接******************************************/
	/**
	 * 添加友情链接
	 * @param array $data
	 */
	public function addLink($data)
	{
		$id = model('pc_friendly_link')->add($data);
		Cache::tag("pc_friendly_link")->clear();
		return $this->success($id);
	}

	/**
	 * 修改友情链接
	 * @param array $data
	 */
	public function editLink($data, $condition)
	{
		$res = model('pc_friendly_link')->update($data, $condition);
		Cache::tag("pc_friendly_link")->clear();
		return $this->success($res);
	}

	/**
	 * 删除友情链接
	 * @param unknown $coupon_type_id
	 */
	public function deleteLink($condition)
	{
		$res = model('pc_friendly_link')->delete($condition);
		Cache::tag("pc_friendly_link")->clear();
		return $this->success($res);
	}

	/**
	 * 获取友情链接详情
	 * @param int $id
	 * @return multitype:string mixed
	 */
	public function getLinkInfo($id)
	{
		$cache = Cache::get("pc_friendly_link_info" . $id);
		if (!empty($cache)) {
			return $this->success($cache);
		}
		$res = model('pc_friendly_link')->getInfo([ [ 'id', '=', $id ] ], 'id, link_title, link_url, link_pic, link_sort, is_blank, is_show');
		Cache::tag("pc_friendly_link")->set("pc_friendly_link_info" . $id, $res);
		return $this->success($res);
	}

	/**
	 * 获取导航分页列表
	 * @param array $condition
	 * @param number $page
	 * @param string $page_size
	 * @param string $order
	 * @param string $field
	 */
	public function getLinkPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'link_sort desc', $field = 'id, link_title, link_url, link_pic, link_sort, is_blank, is_show')
	{
		$data = json_encode([ $condition, $field, $order, $page, $page_size ]);
		$cache = Cache::get("pc_friendly_linkPageList_" . $data);
		if (!empty($cache)) {
			return $this->success($cache);
		}
		$list = model('pc_friendly_link')->pageList($condition, $field, $order, $page, $page_size);
		Cache::tag("pc_friendly_link")->set("pc_friendly_linkPageList_" . $data, $list);
		return $this->success($list);
	}

	/**
	 * 修改排序
	 * @param int $sort
	 * @param int $id
	 */
	public function modifyLinkSort($sort, $id)
	{
		$res = model('pc_friendly_link')->update([ 'link_sort' => $sort ], [ [ 'id', '=', $id ] ]);
		Cache::tag('pc_friendly_link')->clear();
		return $this->success($res);
	}

	/****************************************************************导航******************************************/


}